package cn.tedu.boottext42.controller;

import cn.tedu.boottext42.mapper.UserMapper;
import cn.tedu.boottext42.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired(required = false)
    UserMapper mapper;

    @RequestMapping("/check")
    public int check(String username){
        User user = mapper.selectByusername(username);
        return  user==null?1:2;//这里定义了1没有用户可以用   2用用户不可以用
    }

    @RequestMapping("/reg")
    public int reg (User user){
        User u = mapper.selectByusername(user.getPassword());
        if (u != null){
            return 2;//用户已经存在
        }
        mapper.insert(user);
        return 1;//注册成功
    }

    @RequestMapping("/login")
    public int login(User user){
        User u = mapper.selectByusername(user.getUsername());
        if (u!=null){//数据库的u
            if (u.getPassword().equals(user.getPassword())){
                return 1;//登录成功
            }
            return 3;//密码错误
        }
        return 2;//用户不存在
    }


}
